import workerize from "workerize"
export const worker = workerize(`
    export function getDictName(list,id) {
        if(!list){
            return ""
        }else{
            const obj=list.find(item=>item.id==id)
            return obj?obj.dictLabel:''
        }
    }
    export function findNodePathByKey(list, key) {
        let wPath;
        function getTarget(list, key, absname) {
            if (list && list.length) {
                const len=list.length;
                for (let i = 0; i < len; i++) {
                    const item = list[i];
                    if(item){
                        const path = absname.length ? [...absname, item.name] : [item.name]
                    if (item.id == key) {
                        wPath = path.join("/")
                    }
                    if (item.children && item.children.length) {
                        getTarget(item.children, key, path)
                    }
                    }
                    
                }
            }
            return wPath
        }
        return getTarget(list, key, [])
    }
    export function getTreeNode(data, key) {
        let obj;
        function getOneTarget(data, key) {
            if (data && data.length) {
                const len=data.length;
                for (let i = 0; i < len; i++) {
                    let item = data[i];
                    if (item&&item.id == key) {
                        obj = item;
                        break;
                    } else {
                        if (item&&item.children && item.children.length) {
                            getOneTarget(item.children, key)
                        }
                    }
                }
            }
            return obj;
        }
        return getOneTarget(data, key)
    }
    export function loopTreeNode(data, spanType,sel,disabled) {
        function loop(list) {
            if (list && list.length) {
                const len=list.length;
                for (let i = 0; i < len; i++) {
                    let item = list[i];
                    item.key = String(item.id);
                    item.value = String(item.id);
                    if(item.children&&item.children.length){
                        if(sel){
                            item.selectable=false;
                        }else{
                            item.selectable=true;
                        }
                        
                        item.disabled=disabled?true:false
                       
                    }else{
                        if(sel){
                            item.selectable=true;
                        }else{
                            item.selectable=false;
                        }
                        item.disabled=disabled?false:true
                    }
                    item.title = item.name;
                    if (spanType && item.level == 3 && item.code != spanType) {
                        if(item.code!="F"){
                            delete list[i]
                        }
                        
                    }
                    if (item && item.children && item.children.length) {
                        loop(item.children)
                    } else {
                        continue;
                    }
                }
    
            }
            return list;
        }
        return loop(data)
    }
    export function loopTreeNode2(data, spanType,sel,disabled) {
        function loop(list) {
            if (list && list.length) {
                const len=list.length;
                for (let i = 0; i < len; i++) {
                    let item = list[i];
                    item.key = String(item.id);
                    item.value = String(item.id);
                    if(item.treeLeaf!=0){
                        item.selectable=false;
                        item.disabled=disabled?true:false
                    }else{
                        item.selectable=true
                        item.disabled=disabled?false:true
                    }
                    item.title = item.name;
                    if (spanType && item.level == 3 && item.code != spanType) {
                        delete list[i]
                    }
                    if (item && item.children && item.children.length) {
                        loop(item.children)
                    } else {
                        continue;
                    }
                }
    
            }
            return list;
        }
        return loop(data)
    }
    export function loopTreeNode3(data, ids,sel,disabled) {
        function loop(list) {
            if (list && list.length) {
                const len=list.length;
                for (let i = 0; i < len; i++) {
                    let item = list[i];
                    item.key = String(item.id);
                    item.value = String(item.id);
                    if(item.treeLeaf!=0){
                        item.disabled=disabled?true:false;
                        item.selectable=false;
                    }else{
                        item.disabled=disabled?false:true;
                        item.selectable=true
                    }
                    item.title = item.name;
                    if (item.treeLeaf==0&&ids&&ids.length&&!ids.includes(String(item.id))) {
                      delete list[i]
                    }
                    if (item && item.children && item.children.length) {
                        loop(item.children)
                    } else {
                        continue;
                    }
                }
    
            }
            return list;
        }
        return loop(data)
    }
   
    export function loopTreeNode4(data,disabled) {
        function loop(data) {
            if (data && data.length) {
                const len=data.length;
                for (let i = 0; i < len; i++) {
                    let item = data[i];
                    item.title = item.name;
                    item.key = item.id;
                    item.value = item.id;
                    if (item.children && item.children.length) {
                        if(disabled){
                            item.disabled = true;
                        }
                       
                        loop(item.children)
                    }
                }
            }
            return data
        }
        return loop(data)
    }
    export function loopTreeNode5(data) {
        function loop(data) {
            if (data && data.length) {
                for (let i = 0; i < data.length; i++) {
                    let item = data[i];
                    item.key = item.id + item.name;
                    item.value = item.id;
                    item.title = item.name;
                    if (item.level - 3 < 0) {
                        item.selectable = false
                    }
                    if (item.children && item.children.length) {
                        loop(item.children)
                    } else {
                        continue;
                    }
                }

            }
        }
        loop(data)
    }
    export function loopTreeNode6(data,disabled){
        function loop(data){
            if(data&&data.length){
                for (let i = 0; i < data.length; i++) {
                    let item = data[i];
                    item.title=item.name;
                    item.key=item.id;
                    item.value=item.id;
                    item.checkable=true
                    if(item.children&&item.children.length){
                        item.checkable=false;
                        if(disabled){
                            item.disabled=true;
                        }
                        loop(item.children)
                    }
                }
            }
            return data
        }
        return loop(data)
    }
    export function loopTreeNode7(data, spanType,sel) {
        function loop(list) {
            if (list && list.length) {
                const len=list.length;
                for (let i = 0; i < len; i++) {
                    let item = list[i];
                    item.key = item.id + '';
                    item.title = item.name;
                    if (spanType &&spanType.length&& item.level == 3 && !spanType.includes(item.code)) {
                        delete list[i]
                        
                    }
                    if(item.children&&item.children.length&&sel){
                        item.selectable=false;
                    }else{
                        if(sel){
                            item.selectable=true;
                        }
                    }
                    if (item && item.children && item.children.length) {
                        loop(item.children)
                    } else {
                        continue;
                    }
                }

            }
            return list;
        }
        return loop(data)
    }
    export function buildProjectTree(project = {}) {
        let index = 1;
        let root = {
            ...project,
            title: project ? project.name : '项目名称',
            type: 'project',
            key: project ? 'p' + project.id : index++,
            checkable: false,
            children: project ? [
                // 桥梁工程id
                {
                    title: '项目设置', checkable: false, eventType: 'projectSet', eventKey: 'projectSet', key: 'projectSet' + project.id,
                    children: [
                        {
                            isLeaf: true, projectId: project.id, title: '项目信息', checkable: false, eventType: 'projectInfo', eventKey: 'projectInfo', key: 'projectInfo' + project.id,
                        }
                    ]
                },
                // 引用路线资源
                {
                    title: '道路数据', checkable: false, eventType: 'groupRoads', eventKey: 'groupRoads', key: 'groupRoads' + project.id,
                    children: null
                },
                {
                    title: '桥梁数据', checkable: false, eventType: 'bridgeData', eventKey: 'bridgeData', key: 'bridgeData' + project.id,
                    children: null

                },
            ] : null
        };
        return root;
    }
`);


